package GUI;

import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logic.Member;
import logic.Message;
import logic.Order;


import client.OBL_Client;
import java.awt.Color;
import java.util.ArrayList;

import javax.swing.UIManager;


public class ViewOrder extends JPanel{
	
	private Client_GUI parent;
	private JLabel orderTitle;
	private JLabel oidLBL;
	private JLabel uidLBL;
	private JLabel bidLBL;
	private JLabel orderdateLBL;
	private JLabel statusLBL;
	private Order order;
	
	private ResultDisplay results;
	private JButton cancelOrderBTN;
		
	public ViewOrder(final Client_GUI parent,final Order order,final ResultDisplay results) {
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		this.setBackground(UIManager.getColor("inactiveCaption"));

		this.order = order;
		this.results = results;
		
		
		
		orderTitle = new JLabel("Search Book");
		orderTitle.setForeground(Color.RED);
		orderTitle.setFont(new Font("Tahoma", Font.BOLD, 21));
		orderTitle.setBounds(287, 60, 233, 41);
		add(orderTitle);
		
		JLabel oid = new JLabel("Order ID:");
		oid.setFont(new Font("Tahoma", Font.BOLD, 14));
		oid.setBounds(270, 199, 113, 14);
		add(oid);
		
		JLabel bid = new JLabel("Book ID:");
		bid.setFont(new Font("Tahoma", Font.BOLD, 14));
		bid.setBounds(270, 250, 131, 14);
		add(bid);
		
		JLabel uid = new JLabel("Member ID:");
		uid.setFont(new Font("Tahoma", Font.BOLD, 14));
		uid.setBounds(270, 225, 131, 14);
		add(uid);
		
		JLabel orderdate = new JLabel("Order Date:");
		orderdate.setFont(new Font("Tahoma", Font.BOLD, 14));
		orderdate.setBounds(270, 275, 131, 14);
		add(orderdate);
		
		JLabel status = new JLabel("Status:");
		status.setFont(new Font("Tahoma", Font.BOLD, 14));
		status.setBounds(270, 300, 131, 14);
		add(status);
		
		JButton btnCancel = new JButton("Back");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				parent.RepaintMain(results);
				
				//TODO return to results
				
			}
		});
		btnCancel.setBounds(447, 443, 131, 34);
		add(btnCancel);
		
		oidLBL = new JLabel("");
		oidLBL.setBounds(448, 199, 165, 14);
		add(oidLBL);
		
		uidLBL = new JLabel("");
		uidLBL.setBounds(448, 225, 165, 14);
		add(uidLBL);
		
		bidLBL = new JLabel("");
		bidLBL.setBounds(448, 250, 165, 14);
		add(bidLBL);
		
		orderdateLBL = new JLabel("");
		orderdateLBL.setBounds(448, 275, 165, 14);
		add(orderdateLBL);
		
		statusLBL = new JLabel("");
		statusLBL.setBounds(448, 300, 165, 14);
		add(statusLBL);
		
		cancelOrderBTN = new JButton("Cancel Order");
		cancelOrderBTN.setFont(new Font("Tahoma", Font.BOLD, 13));
		cancelOrderBTN.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				ArrayList<String> indexes = new ArrayList<String>();
				ArrayList<Object> data = new ArrayList<Object>();
				int action = Message.CANCEL_ORDER_REQUEST;
				
				indexes.add("BID");
				data.add(order.getBID());
				
				indexes.add("UID");
				data.add(order.getUID());
				
				
				Message toSend = new Message(indexes, data, action);
				
				if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
					parent.SetLoading(true);
								
				}else{//message send failed
				}
			}
		});
		cancelOrderBTN.setBounds(217, 443, 131, 34);
		add(cancelOrderBTN);
		
		if (parent.client.Privelege != Member.MANAGER_PRIVILIGE){ // only librarian can edit
			cancelOrderBTN.setVisible(true);
		}else{
			cancelOrderBTN.setVisible(false);	
		}
		
		InitBookData();
		
	}
	
	private void InitBookData(){
		orderTitle.setText("Order Information");
		oidLBL.setText(String.valueOf(order.getOID()));
		uidLBL.setText(String.valueOf(order.getUID()));
		bidLBL.setText(String.valueOf(order.getBID()));
		orderdateLBL.setText(order.getOrderDate());
		statusLBL.setText(String.valueOf(order.getstatus()));
	}
}
